// subpages0/newyearGreeting/newyearGreeting.js
var app = getApp();
var bgMusicCtx = wx.createInnerAudioContext();
Page({
  data: {
    curGreetLength:0
  },
  onLoad: function (e) {
    var that = this;
    if (e && e.id) {
      that.setData({
        blessingId: e.id
      })
    }
    if (!app.globalData.plumSession) {
      app.wechatSq(that);
    } else {
      that.requestBlesscfg();
      that.getSlient();
    }
  },
  onShow: function () {

  },
  onHide: function () {
    var that = this;
    bgMusicCtx.pause();
    that.setData({
      isPlaymusic: false
    })
  },
  onUnload: function () {
    var that = this;
    bgMusicCtx.stop();
    that.setData({
      isPlaymusic: false
    })
  },
  initPage: function(){
    var that = this;
    that.onLoad();
    that.onShow();
  },
  getSlient:function(){
    var that = this;
    var slient = app.globalData.slient ? app.globalData.slient:'';
    that.setData({
      slient: slient
    })
    if (slient == 1) {
      wx.hideTabBar()
    }
  },
  getBgmusic: function (e) {
    var that = this;
    var blessCfg = that.data.blessCfg;
    if (blessCfg.music) {
      bgMusicCtx.src = blessCfg.music;
      bgMusicCtx.loop = true;
      bgMusicCtx.play();
      that.setData({
        isPlaymusic: true
      })
    }
  },
  togglePlaymusic: function () {
    var that = this;
    var isPlaymusic = that.data.isPlaymusic;
    if (isPlaymusic) {
      bgMusicCtx.pause();
      isPlaymusic = false;
    } else {
      bgMusicCtx.play();
      isPlaymusic = true;
    }
    that.setData({
      isPlaymusic: isPlaymusic
    })
  },
  requestBlesscfg: function () {
    var that = this;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_blessing_cfg',
        blessingId: that.data.blessingId ? that.data.blessingId : ''
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var blessCfg = res.data.data;
          that.setData({
            blessCfg: blessCfg,
            bainianTxt: blessCfg.blessingName,
            greetTxt: blessCfg.blessingContent
          })
          if (blessCfg.title) {
            wx.setNavigationBarTitle({
              title: blessCfg.title
            });
          }
          that.getBgmusic();
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  inputChange:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    var value = e.detail.value;
    that.setData({
      [type]: value
    })
    if (type =='greetTxt'){
      var curGreetLength = value.length;
      that.setData({
        curGreetLength: curGreetLength
      })
    }
  },
  resetConfirm:function(e){
    var that = this;
    var type = e.currentTarget.dataset.type;
    console.log(type);
    var curVal = that.data[type] ? that.data[type]:'';
    if (!curVal)return;
    wx.showModal({
      title: '提示',
      content: '输入系统默认文案将会清空已编辑好的内容哦~',
      success(res) {
        if (res.confirm) {
          that.setData({
            [type]:''
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  previewGreet:function(){
    var that = this;
    var name = that.data.bainianTxt ? that.data.bainianTxt : '';
    var content = that.data.greetTxt ? that.data.greetTxt : '';
    if (name || content){
      that.saveBless();
    }else{
      that.tomyGreet();
    }
  },
  saveBless: function () {
    var that = this;
    var blessCfg = that.data.blessCfg;
    var defaultName = blessCfg.blessNickname + ' ' + blessCfg.defaultName;
    var defaultContent = blessCfg.defaultContent;
    //发起请求，获取列表列表
    wx.showLoading({
      title: '保存中',
    })
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_blessing_save',
        name: that.data.bainianTxt ? that.data.bainianTxt : defaultName,
        content: that.data.greetTxt ? that.data.greetTxt : defaultContent
      },
      success: function (res) {
        if (res.data.ec == 200) {
          console.log(res.data.data);
          var blessingId = res.data.data.blessingId;
          that.setData({
            blessingId: blessingId
          })
          that.tomyGreet(blessingId);
        } else {
          app.errorTip(that, res.data.em, 2000);
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  tomyGreet: function (blessingId) {
    var id = '';
    if (blessingId){
      id = blessingId
    }
    wx.navigateTo({
      url: '/subpages0/yearGreeting/myGreeting/myGreeting?id='+id,
    })
  },
  backShouye: function () {
    app.backHome();
  },
})